//
//  StorefrontDetailViewController.m
//  LiTianDecoration
//
//  Created by zero on 2018/7/13.
//  Copyright © 2018年 administrator. All rights reserved.
//

#import "StorefrontDetailViewController.h"
#import "MapNavigationViewController.h"
//#import "MallHomeViewController.h"
#import "HSQMallShopCarViewController.h"
#import "HSQMessageListViewController.h"
#import "HSQLoginHomeViewController.h"
#import "HSQGoodsModelView.h"
#import "StoreSectionView.h"
#import "JWStarView.h"
#import "YBPopupMenu.h"

#import "StoreActivityCell.h"
#import "StoreInfoCell.h"
#import "StoreDiscussCell.h"
#import "ExhibitionCell.h"
#import "StoreActivityCell.h"


#import "HSQAccount.h"
#import "HSQAccountTool.h"

#import "XXEvaluateModel.h"
@interface StorefrontDetailViewController ()<UITableViewDataSource,UITableViewDelegate,YBPopupMenuDelegate,HSQGoodsModelViewDelegate,UICollectionViewDelegate,UICollectionViewDataSource,StoreActivityDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tabV;
///总数据源
@property (nonatomic,strong)NSMutableArray *array_dataSource;
///评价数据
@property (nonatomic,strong) NSMutableArray *array_evaluate;
///第一分组
@property (nonatomic,strong) StoreSectionView *sectionZero;
///第二分组
@property (nonatomic,strong) StoreSectionView *sectionOne;
///第三分组
@property (nonatomic,strong) StoreSectionView *sectionTwo;
///头像
@property (weak, nonatomic) IBOutlet UIImageView *img_header;
///背景
@property (weak, nonatomic) IBOutlet UIImageView *img_bg;
///店名
@property (weak, nonatomic) IBOutlet UILabel *label_name;
///地址
@property (weak, nonatomic) IBOutlet UILabel *label_address;
///好评
@property (weak, nonatomic) IBOutlet UILabel *label_Praise;
///星🌟
@property (weak, nonatomic) IBOutlet UIView *view_star;
///扩展
@property (nonatomic,strong) UIButton *button_extend;
///活动展示容器
@property (weak, nonatomic) IBOutlet UICollectionView *coV;
@property (weak, nonatomic) IBOutlet UICollectionViewFlowLayout *flowLayout;
///分页计数
@property (weak, nonatomic) IBOutlet UIPageControl *pageControl;


@end

@implementation StorefrontDetailViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"门店详情";
    [self Voluation];
    [self SetCoVItem];
    [self RequestEvaluate];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:self.button_extend];
   
}
///设置CoV相关属性
- (void)SetCoVItem
{
    if (self.model.chainPromotionList.count == 0) {
        return;
    }
    _pageControl.numberOfPages = self.model.chainPromotionList.count;
    _coV.pagingEnabled = YES;
    
    [_coV scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:50] atScrollPosition:UICollectionViewScrollPositionLeft animated:NO];
    
    _flowLayout.minimumLineSpacing = 0;
    _flowLayout.minimumInteritemSpacing = 0;
    
    _flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    _flowLayout.itemSize = CGSizeMake(KScreenWidth, KScreenHeight-64);
    
    
    
}
/**
 赋值
 */
- (void)Voluation
{
    
    [self.array_dataSource addObjectsFromArray:@[@[],@[@{@"营业时间":_model.chainOpeningTime},@{@"联系电话":_model.chainPhone},@{@"公交线路":_model.chainTrafficLine}],@[]]];
    if (_model.chainPromotionList.count != 0) {
        [self.array_dataSource replaceObjectAtIndex:0 withObject:_model.chainPromotionList];
    }
    [_img_bg sd_setImageWithURL:[NSURL URLWithString:_model.imageSrc1]];
    [_img_header sd_setImageWithURL:[NSURL URLWithString:_model.imageSrc2]];
    _label_name.text = _model.chainName;
    _label_address.text = [NSString stringWithFormat:@"%@%@",_model.areaInfo,_model.address];
    _label_Praise.text = [NSString stringWithFormat:@"客户好评率: %ld %@", (long)_model.chainGoodsRate,@"%"];
    
    JWStarView *star = [[JWStarView alloc]initWithFrame:CGRectMake(0, 0, 100, 15)];
    star.currentScore = _model.chainCriterion;
    star.isIndicator = YES;
    star.rateStyle = HalfStar;
    [_view_star addSubview:star];
}

/**
 右上角菜单
 */
- (void)buttonExtendAction{
    [YBPopupMenu showRelyOnView:self.button_extend titles:@[@"首页",@"购物车",@"我的商城",@"消息"] icons:@[] menuWidth:120 delegate:self];
}
/**
 小菜单代理方法
 
 @param index <#index description#>
 @param ybPopupMenu <#ybPopupMenu description#>
 */
- (void)ybPopupMenuDidSelectedAtIndex:(NSInteger)index ybPopupMenu:(YBPopupMenu *)ybPopupMenu
{
    switch (index) {
        case 0:
            [self.navigationController popToViewController:self.navigationController.viewControllers[1] animated:YES];
            break;
        case 1:
        case 2:
        case 3:
        {
            HSQAccount *account = [HSQAccountTool account];
            
            if (account.token.length == 0)  // 没有登录
            {
                HSQLoginHomeViewController *LoginVC = [[HSQLoginHomeViewController alloc] init];
                [self.navigationController pushViewController:LoginVC animated:YES];
            }else{
                if (index == 1)
                {
                     [self.navigationController pushViewController:[HSQMallShopCarViewController new] animated:YES];
                }else if (index == 2)
                {
                    self.tabBarController.selectedIndex = 4;
                    [self.navigationController popToViewController:self.navigationController.viewControllers[0] animated:NO];
                }
                else
                {
                     [self.navigationController pushViewController:[HSQMessageListViewController new] animated:YES];
                }
            }
        }
            break;
        default:
            break;
    }

    
}


/**
 加入购物车
 
 @param sender <#sender description#>
 */
- (IBAction)AddToCarAction:(id)sender {
    
    HSQGoodsModelView *GuiGeAndCouperView = [HSQGoodsModelView initGoodsModelView];
    
    GuiGeAndCouperView.TypeString = @"1000";
    
    //    GuiGeAndCouperView.Ordinary_DatasDiction = self.DataDiction;
    
    GuiGeAndCouperView.delegate = self;
    
    [GuiGeAndCouperView ShowGoodsModelAndPriceView];
}
///关闭活动详情页面
- (IBAction)buttoCancleAction:(id)sender {
    self.coV.hidden = YES;
    self.pageControl.hidden = YES;
}

#pragma mark ---------------------->taV相关方法<-------------------------
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.array_dataSource.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return section == 0 ? ([self.array_dataSource[section] count] == 0 ? 0 : 1) : [self.array_dataSource[section] count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
        if (indexPath.section == 0) {
            
            StoreActivityCell *cell = (StoreActivityCell*)[tableView dequeueReusableCellWithIdentifier:@"StoreActivityCell"];
            cell.delegate = self;
            cell.array = self.array_dataSource[0];
            return cell;
          
        } else if (indexPath.section == 1){
            NSDictionary *dic = self.array_dataSource[indexPath.section][indexPath.row];
            StoreInfoCell *cell = (StoreInfoCell *)[tableView dequeueReusableCellWithIdentifier:@"StoreInfoCell"];
            cell.label_title.text = [dic allKeys].lastObject;
            cell.label_content.text = [dic allValues].lastObject;
            return cell;
        }else {
            StoreDiscussCell *cell = (StoreDiscussCell *)[tableView dequeueReusableCellWithIdentifier:@"StoreDiscussCell"];
            cell.model = self.array_dataSource[indexPath.section][indexPath.row];
            return cell;
        }
    
   
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [self cellHeight:indexPath];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return [self headViewH:section];
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return [self footerViewH:section];
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return [self headView:section];
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return [UIView new];
}
///delegate 点击事件
- (void)CoVdidSelecItem:(NSInteger)item
{
    _coV.hidden = NO;
    _pageControl.hidden = NO;
}
/**
 计算cell高度

 @param indexPath <#indexPath description#>
 */
- (CGFloat)cellHeight:(NSIndexPath *)indexPath
{
    Evaluate *model = indexPath.section != 3 ? nil: self.array_dataSource[indexPath.section][indexPath.row];
    NSDictionary *dic =   indexPath.section == 1 ? self.array_dataSource[indexPath.section][indexPath.row]:nil;
    return  indexPath.section == 0 ? 120 : indexPath.section == 1 ? 20 + [Utility String:[dic allValues].lastObject Font:12 Width:KScreenWidth-100] :[Utility String:model.content Font:13 Width:KScreenWidth-30] + 84;
}
/**
 返回分组头

 @param section <#section description#>
 @return <#return value description#>
 */
- (UIView *)headView:(NSInteger)section
{
    if (section == 0) {
        return [self.array_dataSource[section] count] == 0 ? [UIView new] : self.sectionZero;
    } else if(section == 1){
        return self.sectionOne;
    }else{
        return [self.array_dataSource[section]count] == 0 ? [UIView new] : self.sectionTwo;
    }
}
/**
 计算头高度
 
 @param section <#indexPath description#>
 */
- (CGFloat)headViewH:(NSInteger)section
{
    if (section == 1) {
        return 32;
    }else{
        return [self.array_dataSource[section] count] == 0 ? 0.1 : 32;
    }
}
/**
 计算底高度
 
 @param section <#indexPath description#>
 */
- (CGFloat)footerViewH:(NSInteger)section
{
    if (section == 0) {
        return [self.array_dataSource[section] count] == 0 ? 0.1 : 10;
    } else if(section == 2){
        return 0.1;
    }else{
        return 10;
    }
}
#pragma mark ---------------------->taV相关方法<-------------------------

#pragma mark ---------------------->coV相关方法<-------------------------
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 100;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.model.chainPromotionList.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ExhibitionCell *cell = (ExhibitionCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"ExhibitionCell" forIndexPath:indexPath];
    cell.model = self.model.chainPromotionList[indexPath.row];
    return cell;
}
// collectionView继承于scrollview 所以我们可用此方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if ([scrollView isKindOfClass:[_coV class]]) {
        // 计算page算法
        int page = (int) (scrollView.contentOffset.x / KScreenWidth + 0.5) % self.model.chainPromotionList.count;
        self.pageControl.currentPage = page;
    }
   
    
}
#pragma mark ---------------------->coV相关方法<-------------------------

/**
 获取评价列表
 */
- (void)RequestEvaluate
{
    NSDictionary *dic = @{
                          
                          @"chainEvaluateList": @[
                                                @{
                                                     @"memberNameFinal":@"Google",
                                                     @"memberAvatarUrl":@"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1533870341&di=98f0c7e0f804862a16f070b58122f18d&imgtype=jpg&er=1&src=http%3A%2F%2Fimg011.hc360.cn%2Fhb%2FMTQ2MjUwNzU1NjMwOS0yMDAwNzQ5MzQz.jpg",
                                                     @"chainName": @"云树之家",
                                                     @"scores":@4.3,
                                                     @"addTime":@"2018-08-03",
                                                     @"content":@"这是一首甜蜜般的小情歌，唱着人们心中的白鸽，我想我很快乐"
                                                },
                                                 @{
                                                     @"memberNameFinal":@"Google",
                                                     @"memberAvatarUrl":@"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1533870341&di=98f0c7e0f804862a16f070b58122f18d&imgtype=jpg&er=1&src=http%3A%2F%2Fimg011.hc360.cn%2Fhb%2FMTQ2MjUwNzU1NjMwOS0yMDAwNzQ5MzQz.jpg",
                                                     @"chainName":@"云树之家",
                                                     @"scores": @4.3,
                                                     @"addTime": @"2018-08-03",
                                                     @"content": @"这是一首甜蜜般的小情歌，唱着人们心中的白鸽，我想我很快乐,这是一首甜蜜般的小情歌，唱着人们心中的白鸽，我想我很快乐,这是一首甜蜜般的小情歌，唱着人们心中的白鸽，我想我很快乐,我想我很快乐"
                                                },
                                                 @{
                                                     @"memberNameFinal": @"Google",
                                                     @"memberAvatarUrl": @"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1533870341&di=98f0c7e0f804862a16f070b58122f18d&imgtype=jpg&er=1&src=http%3A%2F%2Fimg011.hc360.cn%2Fhb%2FMTQ2MjUwNzU1NjMwOS0yMDAwNzQ5MzQz.jpg",
                                                     @"chainName": @"云树之家",
                                                     @"scores": @4.3,
                                                     @"addTime": @"2018-08-03",
                                                     @"content": @"这是一首甜蜜般的小情歌，唱着人们心中的白鸽，我想我很快乐,这是一首甜蜜般的小情歌，唱着人们心中的白鸽，我想我很快乐"
                                                }
                                                ]
                          };
    XXEvaluateModel *model = [XXEvaluateModel yy_modelWithJSON:dic];
    [self.array_evaluate addObjectsFromArray:model.chainEvaluateList];
    [self.array_dataSource insertObject:self.array_evaluate atIndex:self.array_dataSource.count];
    [self.tabV reloadData];
    

//    __weak typeof(self)weakSelf = self;
//    [[HTTPRequest shareApiHelper]GETExhibition:self.view UrlString:Evaluate Parameters:[XXEvaluateModel chainId:(int)self.model.chainId page:1] Success:^(id resultObject) {
//        XXEvaluateModel *model = [XXEvaluateModel yy_modelWithJSON:resultObject];
//        [weakSelf.array_evaluate addObjectsFromArray:model.chainEvaluateList];
//        [weakSelf.array_dataSource insertObject:weakSelf.array_evaluate atIndex:weakSelf.array_dataSource.count];
//        [weakSelf.tabV reloadData];
//    } Error:^{
//
//    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (NSMutableArray *)array_dataSource{
    if (!_array_dataSource) {
        _array_dataSource = [[NSMutableArray alloc]init];
    }
    return _array_dataSource;
}
- (NSMutableArray *)array_evaluate{
    if (!_array_evaluate) {
        _array_evaluate = [[NSMutableArray alloc]init];
    }
    return _array_evaluate;
}

- (StoreSectionView *)sectionZero{
    if (!_sectionZero) {
        _sectionZero = [[StoreSectionView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, 30)];
        _sectionZero.label_title.text = @"促销推荐";
    }
    return _sectionZero;
}
- (StoreSectionView *)sectionOne{
    if (!_sectionOne) {
        _sectionOne = [[StoreSectionView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, 30)];
        _sectionOne.label_title.text = @"门店信息";
    }
    return _sectionOne;
}
- (StoreSectionView *)sectionTwo{
    if (!_sectionTwo) {
        _sectionTwo = [[StoreSectionView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, 30)];
        _sectionTwo.label_title.text = @"客户评价";
    }
    return _sectionTwo;
}

#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    
    if ([segue.identifier isEqualToString:@"location"]) {
        MapNavigationViewController *map = (MapNavigationViewController *)segue.destinationViewController;
        map.model = self.model;
    }
}
- (UIButton *)button_extend{
    if (!_button_extend) {
        _button_extend =[UIButton buttonWithType:UIButtonTypeCustom];
        _button_extend.size = CGSizeMake(30, 30);
        [_button_extend setTitle:@"..." forState:UIControlStateNormal];
        [_button_extend setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [_button_extend addTarget:self action:@selector(buttonExtendAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _button_extend;
}

@end
